<?php
class MY_Image_lib
    extends CI_Image_lib
    {
    function __construct() { parent::__construct(); }

    function create_text_image()
        {

        // Reverse the vertical offset
        // When the image is positioned at the bottom
        // we don't want the vertical offset to push it
        // further down.  We want the reverse, so we'll
        // invert the offset.  Note: The horizontal
        // offset flips itself automatically

        if ($this->wm_vrt_alignment == 'B')
            $this->wm_vrt_offset=$this->wm_vrt_offset * -1;

        if ($this->wm_hor_alignment == 'R')
            $this->wm_hor_offset=$this->wm_hor_offset * -1;

        // Set font width and height
        // These are calculated differently depending on
        // whether we are using the true type font or not
        if ($this->wm_use_truetype == TRUE)
            {
            if ($this->wm_font_size == '')
                $this->wm_font_size='17';

            $fontwidth =$this->wm_font_size - ($this->wm_font_size / 4);
            $fontheight=$this->wm_font_size;
            $this->wm_vrt_offset+=$this->wm_font_size;
            }
        else
            {
            $fontwidth =imagefontwidth($this->wm_font_size);
            $fontheight=imagefontheight($this->wm_font_size);
            }

        // Set base X and Y axis values
        $x_axis=$this->wm_hor_offset + $this->wm_padding;
        $y_axis=$this->wm_vrt_offset + $this->wm_padding;

        // Set verticle alignment
        if ($this->wm_use_drop_shadow == FALSE)
            $this->wm_shadow_distance=0;

        $this->wm_vrt_alignment=strtoupper(substr($this->wm_vrt_alignment, 0, 1));
        $this->wm_hor_alignment=strtoupper(substr($this->wm_hor_alignment, 0, 1));


        //  Fetch source image properties
        if ($this->wm_angel) //если развернуто на 90 градусов
            {
            $this->orig_width =$fontheight + 5;
            $this->orig_height=$fontwidth * strlen($this->wm_text) - $fontwidth;
            }
        else
            {
            $this->orig_width =$fontwidth * strlen($this->wm_text) - $fontwidth;
            $this->orig_height=$fontheight + 5;
            }

        $this->image_type=2;
        $this->size_str  ='width="' . $this->orig_width . '" height="' . $this->orig_height . '"';
        $this->mime_type ='image/jpeg';

        //creating an amige
        $src_img         =imageCreate($this->orig_width, $this->orig_height);
        $black           =imageColorAllocate($src_img, 0, 0, 0);
        $white           =imageColorAllocate($src_img, 255, 0, 0);
        //$red = imageColorAllocate($img, 255, 0, 0);

        imagecolortransparent($src_img, $white);
        //$mf = imageloadfont ('fonts/Arial12BI.phpfont');

        imageFill($src_img, 1, 1, $white);

        if ($this->wm_use_truetype == TRUE AND !file_exists($this->wm_font_path))
            {
            $this->set_error('imglib_missing_font');
            return FALSE;
            }

        //  Fetch source image properties
        //$this->get_image_properties();


        // Set RGB values for text and shadow
        $this->wm_font_color  =str_replace('#', '', $this->wm_font_color);
        $this->wm_shadow_color=str_replace('#', '', $this->wm_shadow_color);

        $R1                   =hexdec(substr($this->wm_font_color, 0, 2));
        $G1                   =hexdec(substr($this->wm_font_color, 2, 2));
        $B1                   =hexdec(substr($this->wm_font_color, 4, 2));

        $R2                   =hexdec(substr($this->wm_shadow_color, 0, 2));
        $G2                   =hexdec(substr($this->wm_shadow_color, 2, 2));
        $B2                   =hexdec(substr($this->wm_shadow_color, 4, 2));

        $txt_color            =imagecolorclosest($src_img, $R1, $G1, $B1);
        $drp_color            =imagecolorclosest($src_img, $R2, $G2, $B2);

        switch ($this->wm_vrt_alignment)
            {
            case "T": break;

            case "M":
                $y_axis+=($this->orig_height / 2) + ($fontheight / 2);

                break;

            case "B":
                $y_axis+=($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2));

                break;
            }

        $x_shad=$x_axis + $this->wm_shadow_distance;
        $y_shad=$y_axis + $this->wm_shadow_distance;

        // Set horizontal alignment
        switch ($this->wm_hor_alignment)
            {
            case "L": break;

            case "R":
                if ($this->wm_use_drop_shadow)
                    $x_shad+=($this->orig_width - $fontwidth * strlen($this->wm_text));

                $x_axis+=($this->orig_width - $fontwidth * strlen($this->wm_text));
                break;

            case "C":
                if (!$this->wm_angel)
                    if ($this->wm_use_drop_shadow)
                        $x_shad+=floor(($this->orig_width - $fontwidth * strlen($this->wm_text)) / 2);

                $x_axis+=floor(($this->orig_width - $fontwidth * strlen($this->wm_text)) / 2);

                break;
            }


        //  Add the text to the source image
        if ($this->wm_use_truetype)
            {
            if ($this->wm_use_drop_shadow)
                imagettftext($src_img,            $this->wm_font_size, $this->wm_angel, $x_shad, $y_shad, $drp_color,
                             $this->wm_font_path, $this->wm_text);

            imagettftext($src_img,            $this->wm_font_size, $this->wm_angel, $x_axis, $y_axis, $txt_color,
                         $this->wm_font_path, $this->wm_text);
            }
        else
            {
            if ($this->wm_use_drop_shadow)
                imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);

            imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
            }

        //  Output the final image
        if ($this->dynamic_output == TRUE)
            {
            $this->image_display_gd($src_img);
            }
        else
            {
            $this->image_save_gd($src_img);
            }

        imagedestroy($src_img);

        return TRUE;
        }

    function create_text()
        {
            $s=substr($this->wm_text, 0, 32);

            //$s="Player Name";
            //$_GET[up]=1;
            if ($this->wm_angel)
                {
                $w=15;
                $h=strlen($s) * 7;
                }
            else
                {
                $w=strlen($s) * 7;
                $h=15;
                }

            $img  =imageCreate($w, $h);

            $black=imageColorAllocate($img, 0, 0, 0);
            $white=imageColorAllocate($img, 255, 255, 255);
            //$red = imageColorAllocate($img, 255, 0, 0);

            imagecolortransparent($img, $white);
            //$mf = imageloadfont ('fonts/Arial12BI.phpfont');

            imageFill($img, 1, 1, $white);

            /*imagettftext($img, 20, 0, 10, 20, $black, "../arial.ttf",
            "Here is PHP text!");
            
            imagettftext($img, 10, 5, 50, 50, $black, "../arial.ttf",
            "PHP Says: «Hello, World!»");
            
            imagettftext($img, 14, 45, 40, 160, $black, "pixcyr2.ttf",
            "Текст с переносами.\n\r
            Перед вами\n\r
            кириллический шрифт\n\r
               под углом 45\n\r
                    градусов");*/

            //imagestring($img, $mf, 20, 60, "Ещё одна PHP", $red);
            //imagestring($img, $mf, 20, 73, "строка", $red);

            //imagestringup($img, 2, 170, 180, "Alternative", $red);

            if ($this->wm_angel)
                {
                // imagestringup($img, 1, 0, 85, $s, $black);
                imagettftext($img, 8, 90, 12, $h - 5, $black, $this->wm_font_path, $this->wm_text);
                }
            else
                {
                imagettftext($img, 8, 0, 0, 12, $black, $this->wm_font_path, $this->wm_text);
                //imagestring($img, 1, 0, 0, $s, $black);
                }

            header('Content-type: image/png');
            imagepng($img);
            imagedestroy($img);
            
        }
    }
?>